# Resource manager
from pyston import *
from random import *

class ResourceManager:
    
    def __init__(self):
        self.loadedImages = {}
        self.loadedAnims = {}
        self.loadedSounds = {}
        self.loadedLevels = {}
        
    def loadResource(self, resourceName, fileName):
        # Determine from globals whether the resource is an image, anim etc
        if fileName.endswith(rs_suffix_images) == True:
            return self.loadImage(resourceName, fileName)
        
    def loadImage(self, resourceName, fileName):
        if fileName.endswith(rs_suffix_images) == True:
            # Get the correct directory
            imagePath = os.path.abspath(os.path.join(directory_root, directory_images, fileName))
            
            # Load and setup the image
            newImage = Image()
            
            if (newImage.load(imagePath) == True):
                debug_out("ResourceManager - Loaded image: " + imagePath)
            else:
                debug_out("ResourceManager - Failed to load image: " + imagePath)
                return None
            
             # If there is no resourceName, use the path
            if resourceName == None:
                resourceName = imagePath
            
            # Add into the loaded images resource
            self.loadedImages[resourceName] = newImage
            
            # Return this should it be required
            debug_out("ResourceManager - Resource: " + resourceName + " created.")
            return self.loadedImages[resourceName]
        else:
            return None
        
    def unloadImage(self, resourceName):
        if self.loadedImages.has_key(resourceName):
            del self.loadedImages[resourceName]
            debug_out("ResourceManager - Unloaded resource: " + resourceName)
        else:
            debug_out("ResourceManager - Image not found: " + resourceName + " (unloadImage)")

    def getImage(self, resourceName):
        return self.loadedImages[resourceName]
            
        
class Animation:
    pass

class Image:
    def __init__(self):
        self.frame = None
        self.rect = None
        self.colorKey = (255, 0, 255)
        
    def load(self, imageFile):
        # Load the image and set the colour key, finally set the rect
        try:
            self.frame = pygame.image.load(imageFile).convert()
        except pygame.error, message:
            return False
           
        self.frame.set_colorkey(self.colorKey, pygame.RLEACCEL)
        self.rect = self.frame.get_rect()
        return True